<?php
Class FZ_Modules {
    
    
    function __construct(){
        
    }
    
    /**
     * Load modules
     * @param string $module_name
     * @return mixed
     */
    function load($module_name){
        
        $error = array();
        $module_name = $this->seoUrl($module_name);
        
        
        //check dir
        if(!$this->check_module_folder($module_name)){
            $error['check_module_folder'] = 'Module folder not exists';
        }
        
        if(!$this->check_module_file($module_name)){
            $error['check_module_file'] = 'Module file not exists';
        }
        
        //load module
        if(count($error) == 0){
            include_once('modules/'.$module_name.'/MODULE_'.$module_name.'.php');
        }
    }
    function seoUrl($module_name) {
        //Lower case everything
        $string = strtolower($module_name);
        //Make alphanumeric (removes all other characters)
        $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
        //Clean up multiple dashes or whitespaces
        $string = preg_replace("/[\s-]+/", " ", $string);
        //Convert whitespaces and underscore to dash
        $string = preg_replace("/[\s_]/", "-", $string);
        return $string;
    }
    /**
     * Check module folder
     * @param string $module_name
     * @return bool
     */
    function check_module_folder($module_name){
        if(file_exists('modules/'.$module_name) && is_dir('modules/'.$module_name)){
            $output = true;
        }else{
            $output = false;
        }
        return $output;
    }
    
    function check_module_file($module_name){
        if (!file_exists('modules/'.$module_name.'/MODULE_'.$module_name.'.php')) {   
            $output = false;                        
        }else{
            $output = true;
        }
        return $output;
    }
    
}

$FZ_Modules = new FZ_Modules;
?>